package test;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.security.KeyException;

import javax.swing.JFrame;

public class MyFrame {

	public static void main(String[] args) {

		JFrame f = new JFrame("一刀999级 超级增强版 飞机大战");// 实例化窗体对象

		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		// 设置窗体默认关闭

		MyPanel mp = new MyPanel();// 实例化画布对象
		f.add(mp);// 将画布添加到窗体

		Thread t = new Thread(mp);// 实例化线程对象
		t.start();// 启动线程

		f.setSize(480, 652);// 设置窗体的大小
		f.setLocationRelativeTo(null);// 设置默认的窗体位置
		f.setVisible(true);// 设置现实窗体

		//添加鼠标动作侦听事件
		f.addMouseMotionListener(new MouseAdapter() {
			
			public void mouseMoved (MouseEvent e){
				mp.plane.x=e.getX()-33;
				mp.plane.y=e.getY()-50;
			}
		});
		
		// 添加键盘监听事件
		f.addKeyListener(new KeyAdapter() {// 键盘配置器

			@Override
			public void keyPressed(KeyEvent e) {// 按下监控
				switch (e.getKeyCode()) {
				case KeyEvent.VK_A:
				case KeyEvent.VK_LEFT:
					mp.plane.left = true;
					break;
				case KeyEvent.VK_RIGHT:
				case KeyEvent.VK_D:
					mp.plane.right = true;
					break;
				case KeyEvent.VK_UP:
				case KeyEvent.VK_W:
					mp.plane.up = true;
					break;
				case KeyEvent.VK_DOWN:
				case KeyEvent.VK_S:
					mp.plane.down = true;
					break;

				case KeyEvent.VK_SPACE:   //空格键 使用炸弹道具
					if(mp.equip.count>0){
						mp.equip.count--;
						mp.flag = 1;
					}
					break;

				case KeyEvent.VK_ENTER:   //开始游戏功能
					mp.plane.flag=0;
					mp.plane.score=0;
					mp.plane.hp=9999;
					mp.equip.count = 0;
					break;

				}
			}
			
			@Override
			public void keyReleased(KeyEvent e){
				switch (e.getKeyCode()) {
				case KeyEvent.VK_A:
				case KeyEvent.VK_LEFT:
					mp.plane.left = false;
					break;
				case KeyEvent.VK_RIGHT:
				case KeyEvent.VK_D:
					mp.plane.right = false;
					break;
				case KeyEvent.VK_UP:
				case KeyEvent.VK_W:
					mp.plane.up = false;
					break;
				case KeyEvent.VK_DOWN:
				case KeyEvent.VK_S:
					mp.plane.down = false;
					break;
				}
			}
				
		});

	}

}
